package im.elephas.androfan.activity;

import im.elephas.androfan.R;
import im.elephas.androfan.fanfouapi.FanfouApi;
import im.elephas.androfan.fanfouapi.events.IVerifyEvent;
import im.elephas.androfan.service.AndrofanService;
import im.elephas.androfan.service.IAndrofanService;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AndroFanActivity implements OnClickListener {

	private static final String SETTING_INFOS = "user_information";

	private CheckBox checkbox;
	private Button loginButton;
	private LoginActivity mAndroFanActivity = this;

	private EditText passwordEdit;

	private EditText usernameEdit;

	@Override
	public void onClick(View v) {
		final String username = usernameEdit.getText().toString();
		final String password = passwordEdit.getText().toString();

		if (username.length() == 0 || password.length() == 0)
			return;

		loginButton.setEnabled(false);

		Toast toast = Toast.makeText(mAndroFanActivity, "正在登陆",
				Toast.LENGTH_SHORT);
		toast.show();

		FanfouApi api = FanfouApi.getInstance();
		api.setAccount(usernameEdit.getText().toString(), passwordEdit
				.getText().toString());
		api.loginAccount(new IVerifyEvent() {

			@Override
			public void verifyHandler(boolean result) {
				if (!result) {
					mAndroFanActivity.runOnUiThread(new Runnable() {

						@Override
						public void run() {
							Builder builder = new AlertDialog.Builder(
									LoginActivity.this);
							builder.setTitle("用户名或密码错误");
							builder.setMessage("用户名和密码输入错误，请检查后重新输入");
							builder.setPositiveButton("确定", null);
							builder.show();
							usernameEdit.setText("");
							passwordEdit.setText("");
							loginButton.setEnabled(true);
						}
					});
					return;
				}

				if (checkbox.isChecked()) {
					SharedPreferences settings = getSharedPreferences(
							SETTING_INFOS, 0);
					settings.edit().putString("username", username)
							.putString("password", password)
							.putBoolean("checkbox", true).commit();
				} else {
					SharedPreferences settings = getSharedPreferences(
							SETTING_INFOS, 0);
					settings.edit().putString("username", "")
							.putString("password", "")
							.putBoolean("checkbox", false).commit();
				}

				Intent s = new Intent(LoginActivity.this, AndrofanService.class);
				ServiceConnection sc = new ServiceConnection() {
					
					@Override
					public void onServiceDisconnected(ComponentName name) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onServiceConnected(ComponentName name, IBinder service) {
						((AndrofanService.AndrofanBinder) service).startReplyTimelineCheck(1000*10*60);
					}
				};
				bindService(s, sc, Context.BIND_AUTO_CREATE);
				unbindService(sc);
				
				s = new Intent(LoginActivity.this, AndrofanService.class);
				sc = new ServiceConnection() {
					
					@Override
					public void onServiceDisconnected(ComponentName name) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onServiceConnected(ComponentName name, IBinder service) {
						((AndrofanService.AndrofanBinder) service).startFriendsTimelineCheck(1000*10*60);
					}
				};
				bindService(s, sc, Context.BIND_AUTO_CREATE);
				unbindService(sc);
				
				Intent i = new Intent(LoginActivity.this,
						FriendsStatusActivity.class);
				startActivityForResult(i, 0);

				finish();
			}
		});
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.androfan);
		setTitle("AndroFan 登陆");

		loginButton = (Button) findViewById(R.id.loginButton);
		usernameEdit = (EditText) findViewById(R.id.usernameEdit);
		passwordEdit = (EditText) findViewById(R.id.passwordEdit);
		checkbox = (CheckBox) findViewById(R.id.loginCheckBox);

		SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0);
		String username = settings.getString("username", "");
		String password = settings.getString("password", "");
		Boolean check = settings.getBoolean("checkbox", false);
		usernameEdit.setText(username);
		passwordEdit.setText(password);
		checkbox.setChecked(check);

		loginButton.setOnClickListener(this);

		Intent s = new Intent(LoginActivity.this, AndrofanService.class);
		startService(s);
	}
}
